package net.zjitc.controller;

import net.zjitc.domain.CartItem;
import net.zjitc.domain.Category;
import net.zjitc.domain.Product;
import net.zjitc.domain.User;
import net.zjitc.service.CartService;
import net.zjitc.service.Impl.CartServiceImpl;
import net.zjitc.service.Impl.ServiceImpl;
import net.zjitc.service.Service;
import org.apache.commons.beanutils.BeanUtils;
import utils.dispacher.URLMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/11.
 */
public class UserCart {
CartService service=new CartServiceImpl();
    Service ser=new ServiceImpl();
    @URLMapping(url = "/MyCart")
    public String MyCart(HttpServletRequest request, HttpServletResponse response) {
        List<Category> clist = ser.findAllCategory();
        request.setAttribute("clist",clist);
        User user = (User)request.getSession().getAttribute("user");
        if(user==null)
        {
            return "/WEB-INF/user/login.jsp";
        }
            List<CartItem> list = service.findByUid(user.getUid());
            request.setAttribute("cartList", list);
            double total = 0.0;
            if(list==null)
            {
                return "/WEB-INF/user/cart.jsp";
            }
            else {
                for (CartItem i : list) {
                    total = total + i.getQuantity() * i.getProduct().getShop_price();
                }
                request.setAttribute("total", total);
                return "/WEB-INF/user/cart.jsp";
            }

    }
    @URLMapping(url = "/addcart")
    public String addCart(HttpServletRequest request, HttpServletResponse response) {
//
//        List<CartItem>list=service.fingByUid(user.getUid());
//        request.setAttribute("cartList",list);
            User user = (User)request.getSession().getAttribute("user");

        Map<String,String[]> map=request.getParameterMap();
            CartItem cartItem=new CartItem();
        Product product=new Product();
        if(user==null)
        {
            return "/WEB-INF/user/login.jsp";
        }
        else {
            try {
                BeanUtils.populate(cartItem,map);
                BeanUtils.populate(product,map);
                cartItem.setUser(user);
                cartItem.setProduct(product);
                int count=service.add(cartItem);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        String url=request.getContextPath()+"/MyCart";
        return "Redirect:"+url;
    }
    @URLMapping(url = "/deletecart")
    public String daleteCart(HttpServletRequest request, HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");
        if(user==null)
        {
            return "/WEB-INF/user/login.jsp";
        }
        int pid = Integer.valueOf(request.getParameter("pid"));
        int num= service.delete(pid);
        String url=request.getContextPath()+"/MyCart";
        return "Redirect:"+url;
    }
    @URLMapping(url = "/clearcart")
    public String clearCart(HttpServletRequest request, HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");
        if(user==null)
        {
            return "/WEB-INF/user/login.jsp";
        }
       int num= service.clear();
        String url=request.getContextPath()+"/MyCart";
        return "Redirect:"+url;
    }
}
